package com.hndzyu.yd.vip;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.MyPDD;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.utils.Util;
import com.hndzyu.yd.widget.SignDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zz9527 on 2018/12/18.
 */
public class MyPDDActivity extends Activity {
    private TextView keti;
    private TextView fukuannum;
    private TextView yuji;
    private TextView yongjin;
    private RecyclerView lv;
    private MuchMoreOrderAdapter muchMoreOrderAdapter;
    private TextView ts;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypdd);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        findViewById(R.id.top_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        keti = findViewById(R.id.keti);
        fukuannum = findViewById(R.id.fukuannum);
        yuji = findViewById(R.id.yuji);
        yongjin = findViewById(R.id.yongjin);
        lv = findViewById(R.id.lv);
        lv.setLayoutManager(new LinearLayoutManager(MyPDDActivity.this));
        lv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        muchMoreOrderAdapter = new MuchMoreOrderAdapter(MyPDDActivity.this);
        lv.setAdapter(muchMoreOrderAdapter);
        SmartRefreshLayout refresh = findViewById(R.id.refresh);
        refresh.setRefreshHeader(new ClassicsHeader(this));
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                page = 1;
                getData();
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示刷新失败
            }
        });
        ts = findViewById(R.id.ts);
        getData();
        final View zhuanru = findViewById(R.id.zhuanru);
        findViewById(R.id.zhuanru).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final SignDialog dialog = new SignDialog(MyPDDActivity.this);
                if ("0.0".equals(keti.getText().toString())) {
                    dialog.setListener(new SignDialog.SignListener() {
                        @Override
                        public void checktrue() {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                    dialog.showMsg("可提佣金不足");
                    return;
                }
                zhuanru.setClickable(false);
                OkHttpDownloadJsonUtil.downloadJson(MyPDDActivity.this, Path.TOYE(UserBean.uid, keti.getText().toString()), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                    @Override
                    public void onsendJson(String json) {

                        try {
                            JSONObject object = new JSONObject(json);
                            if (object.optString("info").contains("success")) {
                                dialog.setListener(new SignDialog.SignListener() {
                                    @Override
                                    public void checktrue() {
                                        dialog.dismiss();
                                    }
                                });
                                dialog.show();
                                dialog.showMsg("转入成功");
                                getData();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    int page = 1;

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.MYPDD(UserBean.uid, page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {
                    Gson gson = new Gson();
                    MyPDD myPDD = gson.fromJson(json, MyPDD.class);
                    keti.setText(Util.Sub((double) myPDD.getJin() / 100 * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + ""));
                    fukuannum.setText(myPDD.getOrdernum() + "");
                    yuji.setText(Util.Sub((double) myPDD.getOrdersum() / 100 * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + ""));
                    yongjin.setText(Util.Sub((double) myPDD.getJin() / 100 * (Double.parseDouble(UserBean.vipdivide) + Double.parseDouble(UserBean.divide)) + "元"));
                    ts.setText(myPDD.getKefu());
                    ts.setSelected(true);
                    if (myPDD.getData().getData().size() == 0) {
                        Toast.makeText(MyPDDActivity.this, "暂无订单", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    muchMoreOrderAdapter.updateData(page, myPDD.getData().getData());
                } catch (Exception e) {
                    Toast.makeText(MyPDDActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
